use dataset_county, clear

gen varname = ""
foreach x in pre0 pre1 prediff post0 post1 postdiff ddraw {
	gen `x'b = .
	gen `x'se = .
}

preserve

	local j = 1
	foreach x in turnout demvoteshare competition medianincome gini ///
	    unemployment college netmig deathrate {

		reg `x'2000 frack, robust
		replace pre0b = _b[_cons] if _n == `j'
		replace pre1b = _b[_cons] + _b[frack] if _n == `j'
		replace prediffb = _b[frack] if _n == `j'
		replace prediffse = _se[frack] if _n == `j'
		
		replace varname = "`x'" if _n == `j'

		local j = `j' + 1
		
	}

	keep if !mi(pre0b)

	gen predifft = prediffb/prediffse
	gen prediffp = 2*(1-normal(abs(predifft)))

	foreach x in pre0b pre0se pre1b pre1se prediffb prediffse ///
		post1b post1se postdiffb postdiffse ddrawb ddrawse {
		tostring `x', replace format("%20.02fc") force
	}

	foreach x in prediff {
		replace `x'b = `x'b + "*" if `x'p < 0.05
		replace `x'b = `x'b + "*" if `x'p < 0.01
		replace `x'b = `x'b + "*" if `x'p < 0.001
	}

	foreach x in prediffse {
		replace `x' = "(" + `x' + ")"
	}
	foreach x in pre0se pre1se {
		replace `x' = " "
	}

	gen rownumber = _n

	reshape i varname
	reshape j est b se
	reshape xi rownumber
	reshape xij pre0 pre1 prediff   
	reshape long

	sort rownumber est

	replace varname = "Turnout" if varname == "turnout"
	replace varname = "Dem. Pct." if varname == "demvoteshare"
	replace varname = "Competition" if varname == "competition"
	replace varname = "Income (\\$1,000s)" if varname == "medianincome"
	replace varname = "Gini" if varname == "gini"
	replace varname = "Unemployment" if varname == "unemployment"
	replace varname = "College Pct." if varname == "college"
	replace varname = "Migration" if varname == "netmig"
	replace varname = "Drug Deaths" if varname == "deathrate"

	replace varname = "" if est == "se"

	keep varname pre0 pre1 prediff 
	l

	#delimit;

	listtex 
		using "_output/table1a.tex"
		,
		replace
		type rstyle(tabular)
		head(
		\begin{tabular*}
		{.8\hsize}{@{\hskip\tabcolsep\extracolsep\fill}l*{4}{l}}
		\toprule & (1) & (2) & (2) - (1) \\
		& Never fracked & Ever fracked & Difference \\
		\midrule
		)
		foot("\bottomrule\end{tabular*}")
		;
		
	#delimit cr
	
restore

preserve

	keep if highf == 1

	local j = 1
	foreach x in turnout demvoteshare competition medianincome gini ///
	    unemployment college netmig deathrate {

		reg `x'2000 frack, robust
		replace pre0b = _b[_cons] if _n == `j'
		replace pre1b = _b[_cons] + _b[frack] if _n == `j'
		replace prediffb = _b[frack] if _n == `j'
		replace prediffse = _se[frack] if _n == `j'
		
		replace varname = "`x'" if _n == `j'

		local j = `j' + 1
		
	}


	keep if !mi(pre0b)

	gen predifft = prediffb/prediffse
	gen prediffp = 2*(1-normal(abs(predifft)))

	foreach x in pre0b pre0se pre1b pre1se prediffb prediffse ///
		post1b post1se postdiffb postdiffse ddrawb ddrawse {
		tostring `x', replace format("%20.02fc") force
	}

	foreach x in prediff {
		replace `x'b = `x'b + "*" if `x'p < 0.05
		replace `x'b = `x'b + "*" if `x'p < 0.01
		replace `x'b = `x'b + "*" if `x'p < 0.001
	}

	foreach x in prediffse {
		replace `x' = "(" + `x' + ")"
	}
	foreach x in pre0se pre1se {
		replace `x' = " "
	}

	gen rownumber = _n

	reshape i varname
	reshape j est b se
	reshape xi rownumber
	reshape xij pre0 pre1 prediff   
	reshape long

	sort rownumber est

	replace varname = "Turnout" if varname == "turnout"
	replace varname = "Dem. Pct." if varname == "demvoteshare"
	replace varname = "Competition" if varname == "competition"
	replace varname = "Income (\\$1,000s)" if varname == "medianincome"
	replace varname = "Gini" if varname == "gini"
	replace varname = "Unemployment" if varname == "unemployment"
	replace varname = "College Pct." if varname == "college"
	replace varname = "Migration" if varname == "netmig"
	replace varname = "Drug Deaths" if varname == "deathrate"

	replace varname = "" if est == "se"

	keep varname pre0 pre1 prediff 
	l

	#delimit;

	listtex 
		using "_output/table1b.tex"
		,
		replace
		type rstyle(tabular)
		head(
			\begin{tabular*}
			{.8\hsize}{@{\hskip\tabcolsep\extracolsep\fill}l*{4}{l}}
			\toprule & (1) & (2) & (2) - (1) \\
			& Never fracked & Ever fracked & Difference \\
			\midrule
		)
		foot("\bottomrule\end{tabular*}")
		;
		
	#delimit cr
	
restore


